/**
 * @fileOverview Castle游戏状态机类源文件
 * @author <a href="http://sivory.my.phpcloud.com">Sivory</a>
 * @version 0.0.0
 */

(function() {

	/** @exports _Class as Name.Sivory.Castle.Core.Status */
	var _Class =

	/**
	 * @class Castle游戏实例类Game。它托管着一个游戏集成环境，保证游戏各部件的协同工作。游戏的主循环在此类中托管，另外，Game还负责游戏不同界面间的切换。
	 * @param {Name.Sivory.Castle.Core.Game} game 游戏实例对象，参见{@link Name.Sivory.Castle.Core.Game}
	 * @author <a href="http://sivory.my.phpcloud.com">Sivory</a>
	 * @version 0.0.1
	 */
	Name.Sivory.Castle.Core.Status = function(g) {

			/**
			 * @private
			 * @field
			 * @type Name.Sivory.Castle.Core.Game
			 * @description 游戏实例的引用
			 * @since 0.0.1
			 */
			this.game = g;

			/**
			 * @public
			 * @field
			 * @type Name.Sivory.Castle.Core.Interface
			 * @description 游戏中当前处于活动状态的界面
			 * @since 0.0.1
			 */
			this.activeInterface = null;

			/**
			 * @public
			 * @field
			 * @type Name.Sivory.Castle.Util.HashTable
			 * @description 游戏中的用户数据（用户可见）
			 * @since 0.0.1
			 */
			this.userData = new Name.Sivory.Castle.Util.HashTable();

			/**
			 * @public
			 * @field
			 * @type Name.Sivory.Castle.Util.HashTable
			 * @description 游戏中的内核数据（用户不可见）
			 * @since 0.0.1
			 */
			this.systemData = new Name.Sivory.Castle.Util.HashTable();

			/**
			 * @public
			 * @field
			 * @type Name.Sivory.Castle.Util.HashTable
			 * @description 游戏运行时的键盘状态
			 * @since 0.0.1
			 */
			this.keyboardStatus = new Name.Sivory.Castle.Util.HashTable();

			/**
			 * @public
			 * @field
			 * @type Name.Sivory.Castle.Core.Active
			 * @description 游戏中的主角活动体
			 * @since 0.0.1
			 */
			this.leadingRole = null;

			/**
			 * @public
			 * @field
			 * @type Array
			 * @description 游戏中的配角活动体集合
			 * @since 0.0.1
			 */
			this.supportingRoles = [];

			/**
			 * @public
			 * @field
			 * @type Name.Sivory.Castle.Core.Team
			 * @description 游戏中的主角属性托管单元
			 * @since 0.0.1
			 */
			this.team = null;

			/**
			 * @public
			 * @field
			 * @type Name.Sivory.Castle.Core.Event
			 * @description 游戏事件托管
			 * @since 0.0.1
			 */
			this.event = new Name.Sivory.Castle.Core.Event();
		};

			/**
			 * @public
			 * @function
			 * @description 游戏事件托管
			 * @param {String} key 设置key键已被按下
			 * @since 0.0.1
			 */
			_Class.prototype.getKeyboardStatus = function(key) {
				if(keyboardStatus.containsKey(key)) {
					return keyboardStatus.get(key);
				} else {
					return false;
				}
			};
			
			_CLass.prototype.setKeyboardStatus = function(key, pressed) {
				keyboardStatus.put(key, pressed);
			};

			this.resetKeyboardStatus = function() {
				keyboardStatus.clear();
			};

			this.checkKeyPressed = function(key) {
				var keyStatus = game.status.getKeyboardStatus(key);
				var time = (new Date()).getTime();
				if(!keyStatus) return false;
				if(keyStatus < time) {
					this.setKeyboardStatus(key, time + game.config.keyDelay);
					return keyStatus;
				}
				return false;
			}

			this.checkKeyHold = function(key) {
				var keyStatus = game.status.getKeyboardStatus(key);
				if(!keyStatus) return false;
				return keyStatus;
			}

			this.saveToLocal = function() {
				//存储场景数据
				var sceneKey = "movcastle.com|" + game.config.gameKey + "|scene";
				var sceneData = this.activeInterface.encode();
				localStorage.setItem(sceneKey, sceneData);
				//存储全局变量
				var globalKey = "movcastle.com|" + game.config.gameKey + "|global";
				var globalData = this.V.encode();
				localStorage.setItem(globalKey, globalData);
				//存储角色数据
				var teamKey = "movcastle.com|" + game.config.gameKey + "|team";
				var teamData = this.team.encode();
				localStorage.setItem(teamKey, teamData);
			}

			this.loadFromLocal = function() {
				//读取场景数据
				var sceneKey = "movcastle.com|" + game.config.gameKey + "|scene";
				var sceneData = localStorage.getItem(sceneKey);
				this.activeInterface.decode(sceneData);
				//读取全局变量
				var globalKey = "movcastle.com|" + game.config.gameKey + "|global";
				var globalData = localStorage.getItem(globalKey);
				this.V.decode(globalData);
				//存储角色数据
				var teamKey = "movcastle.com|" + game.config.gameKey + "|team";
				var teamData = localStorage.getItem(teamKey);
				this.team.decode(teamData);
			}

			this.reset = function() {
				this.V.clear();
				this.leadingRole = null;
				this.supportingRoles.splice(0);
				this.team = null;
				this.clearEventHandlers();
				this.resetKeyboardStatus();
			}

			/**
			 * @function
			 * @public
			 * @description 切换界面的方法。当游戏需要进行界面切换时，调用此方法。
			 * @param {String} id 界面资源的id，此id需要在游戏源中声明。
			 * @param {String} type 界面资源的类型，目前有四个可选值，分别是"scene"，"menu"，"battle"，"load"，前三种分别是三种基本界面类型，load表示读取存档，此时将会加载scene类型的界面（因为保存的时候必然是在场景中进行保存操作的，而战斗中则不能进行保存操作，保存菜单界面也根本没有意义）
			 * @param {Boolean} cbgm 是否切换当前的背景音乐
			 * @since 0.0.1
			 */
			this.changeInterface = function(id, type, cbgm) {
				//在当前循环体结束时打开加载画面
				instance.loopEndHooks.push(function() {
					instance.loader.start();
				});
				//停止主循环
				loopStoped = true;
				//清空资源集合
				instance.resource.clear();
				//清空状态机中注册的事件
				instance.status.clearEventHandlers();
				//当要读取进度时
				if(type == "load") {
					var sceneKey = "movcastle.com|" + instance.config.gameKey + "|scene";
					//
					var sceneStr = localStorage.getItem(sceneKey);
					if(!sceneStr) {
						throw "存档读取错误";
					} else {
						var sceneData = eval("(" + sceneStr + ")");
						var interfaceBuilder = OS_Resource.Builder.get("scene");
						var newInterface = new interfaceBuilder(instance, sceneData.sceneid);
						var activeBuilder = OS_Resource.Builder.get("active");
						new activeBuilder(instance, sceneData.activeid);
						instance.loopEndHooks.push(function() {
							instance.status.loadFromLocal();
						});
					}
				} else {
					var interfaceBuilder = OS_Resource.Builder.get(type);
					var newInterface = new interfaceBuilder(instance, id);
				}
				newInterface.show();
				OS_Resource.loadResource(instance, instance.loader, function() {
					instance.status.loadTrigger.active(); //触发加载完毕事件
					instance.loader.stop(); //停止加载器界面
					if(cbgm == null || cbgm == true) newInterface.playBGM(); //播放背景音乐
					delete window.$starttime;
					//启动主循环
					instance.mainLoop();
				});
			};

			this.createInterface = function() {
				//新建一个入口界面
				var entry = new OS_Menu(this, this.config.entryInterface);
				this.loader.start();
				entry.show();
				OS_Resource.loadResource(this, this.loader, function() {
					this.loader.stop();
					entry.playBGM();
					this.mainLoop();
				});
			}
})();